package org.lhh.server.core.codec.trans;

import org.lhh.server.common.utils.CollectionUtils;
import org.pentaho.di.partition.PartitionSchema;

import java.util.ArrayList;
import java.util.List;

public class PartitionSchemaCodec {

    private String name;
    private String dynamic;
    private String partitions_per_slave;
    private List<String> partition;

    public static List<PartitionSchemaCodec> encode(List<PartitionSchema> partitionSchemas){
        List<PartitionSchemaCodec> partitionSchemaCodecs=new ArrayList<>();
        if(!CollectionUtils.isEmpty(partitionSchemas)){
            for(PartitionSchema partitionSchema:partitionSchemas){
                PartitionSchemaCodec partitionSchemaCodec = new PartitionSchemaCodec();
                partitionSchemaCodec.setName(partitionSchema.getName());
                partitionSchemaCodec.setDynamic(partitionSchema.isDynamicallyDefined() ? "Y" : "N");
                partitionSchemaCodec.setPartitions_per_slave(partitionSchema.getNumberOfPartitionsPerSlave());
                partitionSchemaCodec.setPartition(partitionSchema.getPartitionIDs());
                partitionSchemaCodecs.add(partitionSchemaCodec);
            }
        }
        return partitionSchemaCodecs;
    }

    public static List<PartitionSchema> decode(List<PartitionSchemaCodec> partitionSchemaCodecs){
        List<PartitionSchema> partitionSchemas=new ArrayList<>();
        if(!CollectionUtils.isEmpty(partitionSchemaCodecs)){
            for(PartitionSchemaCodec partitionSchemaCodec:partitionSchemaCodecs){
                PartitionSchema partitionSchema = new PartitionSchema();
                partitionSchema.setName(partitionSchemaCodec.getName());
                partitionSchema.setDynamicallyDefined("Y".equalsIgnoreCase(partitionSchemaCodec.getDynamic()));
                partitionSchema.setNumberOfPartitionsPerSlave(partitionSchemaCodec.getPartitions_per_slave());
                partitionSchema.setPartitionIDs(partitionSchemaCodec.getPartition());
                partitionSchemas.add(partitionSchema);
            }
        }
        return partitionSchemas;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDynamic() {
        return dynamic;
    }

    public void setDynamic(String dynamic) {
        this.dynamic = dynamic;
    }

    public String getPartitions_per_slave() {
        return partitions_per_slave;
    }

    public void setPartitions_per_slave(String partitions_per_slave) {
        this.partitions_per_slave = partitions_per_slave;
    }

    public List<String> getPartition() {
        return partition;
    }

    public void setPartition(List<String> partition) {
        this.partition = partition;
    }
}
